var clusterStyles = [
    {
        url:'http://wy.pa1.cn/resources/image/c1.png',
        size:[42, 36], //图片大小
        offset:new T.Point(-15, -13), //显示图片的偏移量
        textColor:'#fff', //显示数字的颜色
        textSize:10,//显示文字的大小
        range:[0, 50],
    }, {
    url:'http://wy.pa1.cn/resources/image/c2.png',
    size:[42, 36],
    offset:new T.Point(-20, -17),
    textColor:'#fff',
    textSize:10,
    range:[50, 100],
}, {
    url:'http://wy.pa1.cn/resources/image/c3.png',
    size:[52, 46],
    soffset:new T.Point(-10, -22),
    textColor:'#fff',
    textSize:12,
    range:[100, 200],
} , {
        url:'http://wy.pa1.cn/resources/image/c4.png',
        size:[62, 56],
        soffset:new T.Point(-10, -22),
        textColor:'#fff',
        textSize:12,
        range:[200, 300],
    }, {
        url:'http://wy.pa1.cn/resources/image/c5.png',
        size:[52, 46],
        soffset:new T.Point(-10, -22),
        textColor:'#fff',
        textSize:14,
        range:[300, 1000],
    }
]

function localSearchResult(result) {
    //清空地图及搜索列表
    clearAll();
    //根据返回类型解析搜索结果
    switch (parseInt(result.getResultType())) {
        case 1:
            //解析点数据结果
            pois(result.getPois());
            break;
        case 2:
            //解析推荐城市
            statistics(result.getStatistics());
            break;
        case 3:
            //解析行政区划边界
            area(result.getArea());
            break;
        case 4:
            //解析建议词信息
            suggests(result.getSuggests());
            break;
        case 5:
            //解析公交信息
            lineData(result.getLineData());
            break;
    }
}

//解析提示词
function prompt(obj) {
    var prompts = obj.getPrompt();
    if (prompts) {
        var promptHtml = "";
        for (var i = 0; i < prompts.length; i++) {
            var prompt = prompts[i];
            var promptType = prompt.type;
            var promptAdmins = prompt.admins;
            var meanprompt = prompt.DidYouMean;
            if (promptType == 1) {
                promptHtml += "<p>您是否要在" + promptAdmins[0].name + "</strong>搜索更多包含<strong>" + obj.getKeyword() + "</strong>的相关内容？<p>";
            }
            else if (promptType == 2) {
                promptHtml += "<p>在<strong>" + promptAdmins[0].name + "</strong>没有搜索到与<strong>" + obj.getKeyword() + "</strong>相关的结果。<p>";
                if (meanprompt) {
                    promptHtml += "<p>您是否要找：<font weight='bold' color='#035fbe'><strong>" + meanprompt + "</strong></font><p>";
                }
            }
            else if (promptType == 3) {
                promptHtml += "<p style='margin-bottom:3px;'>有以下相关结果，您是否要找：</p>"
                for (i = 0; i < promptAdmins.length; i++) {
                    promptHtml += "<p>" + promptAdmins[i].name + "</p>";
                }
            }
        }
        if (promptHtml != "") {
            document.getElementById("promptDiv").style.display = "block";
            document.getElementById("promptDiv").innerHTML = promptHtml;
        }
    }
}

//解析点数据结果
function pois(obj) {
    if (obj) {
        //显示搜索列表
        var divMarker = document.createElement("div");
        //坐标数组，设置最佳比例尺时会用到
        var zoomArr = [];
        for (var i = 0; i < obj.length; i++) {
            //闭包
            (function (i) {
                //名称
                var name = obj[i].name;
                //地址
                var address = obj[i].address;
                //坐标
                var lnglatArr = obj[i].lonlat.split(" ");
                var lnglat = new T.LngLat(lnglatArr[0], lnglatArr[1]);

                var winHtml = "名称:" + name + "<br/>地址:" + address;

                //创建标注对象
                var marker = new T.Marker(lnglat);
                //地图上添加标注点

                zoomArr.push(lnglat);

                //在页面上显示搜索的列表
                var a = document.createElement("a");
                a.href = "javascript://";
                a.innerHTML = name;
                a.style.width="100%";
                a.style.display="block";
                a.onclick = function () {
                    showPosition(marker, winHtml);
                }
                divMarker.appendChild(a);

            })(i);
        }
        //显示地图的最佳级别
        //map.setViewport(zoomArr);
        //显示搜索结果
        document.getElementById("searchDiv").appendChild(divMarker);
        document.getElementById("resultDiv").style.display = "block";
    }
}

//显示信息框
function showPosition(marker, winHtml) {
    /*map.clearOverLays();*/
    /*map.addOverLay(marker);*/
    map.centerAndZoom(marker.getLngLat (), 22);
   /* var markerInfoWin = new T.InfoWindow(winHtml, {autoPan: true});
    marker.openInfoWindow(markerInfoWin);*/
}

//解析推荐城市
function statistics(obj) {
    if (obj) {
        //坐标数组，设置最佳比例尺时会用到
        var pointsArr = [];
        var priorityCitysHtml = "";
        var allAdminsHtml = "";
        var priorityCitys = obj.priorityCitys;
        if (priorityCitys) {
            //推荐城市显示
            priorityCitysHtml += "在中国以下城市有结果<ul>";
            for (var i = 0; i < priorityCitys.length; i++) {
                priorityCitysHtml += "<li>" + priorityCitys[i].name + "(" + priorityCitys[i].count + ")</li>";
            }
            priorityCitysHtml += "</ul>";
        }

        var allAdmins = obj.allAdmins;
        if (allAdmins) {
            allAdminsHtml += "更多城市<ul>";
            for (var i = 0; i < allAdmins.length; i++) {
                allAdminsHtml += "<li>" + allAdmins[i].name + "(" + allAdmins[i].count + ")";
                var childAdmins = allAdmins[i].childAdmins;
                if (childAdmins) {
                    for (var m = 0; m < childAdmins.length; m++) {
                        allAdminsHtml += "<blockquote>" + childAdmins[m].name + "(" + childAdmins[m].count + ")</blockquote>";
                    }
                }
                allAdminsHtml += "</li>"
            }
            allAdminsHtml += "</ul>";
        }
        document.getElementById("statisticsDiv").style.display = "block";
        document.getElementById("statisticsDiv").innerHTML = priorityCitysHtml + allAdminsHtml;
    }
}

//解析行政区划边界
function area(obj) {
    if (obj) {
        if(obj.points){
            //坐标数组，设置最佳比例尺时会用到
            var pointsArr = [];
            var points = obj.points;
            for (var i = 0; i < points.length; i++) {
                var regionLngLats = [];
                var regionArr = points[i].region.split(",");
                for (var m = 0; m < regionArr.length; m++) {
                    var lnglatArr = regionArr[m].split(" ");
                    var lnglat = new T.LngLat(lnglatArr[0], lnglatArr[1]);
                    regionLngLats.push(lnglat);
                    pointsArr.push(lnglat);
                }
                //创建线对象
                var line = new T.Polyline(regionLngLats, {
                    color: "blue",
                    weight: 3,
                    opacity: 1,
                    lineStyle: "dashed"
                });
                //向地图上添加线
                map.addOverLay(line);
            }
            //显示最佳比例尺
            map.setViewport(pointsArr);
        }
        if(obj.lonlat){
            var regionArr = obj.lonlat.split(",");
            map.panTo(new T.LngLat(regionArr[0], regionArr[1]));
        }
    }
}

//解析建议词信息
function suggests(obj) {
    if (obj) {
        //建议词提示，如果搜索类型为公交规划建议词或公交站搜索时，返回结果为公交信息的建议词。
        var suggestsHtml = "建议词提示<ul>";
        for (var i = 0; i < obj.length; i++) {
            suggestsHtml += "<li>" + obj[i].name + "&nbsp;&nbsp;<font style='color:#666666'>" + obj[i].address + "</font></li>";
        }
        suggestsHtml += "</ul>";
        document.getElementById("suggestsDiv").style.display = "block";
        document.getElementById("suggestsDiv").innerHTML = suggestsHtml;
    }
}

//解析公交信息
function lineData(obj) {
    if (obj) {
        //公交提示
        var lineDataHtml = "公交提示<ul>";
        for (var i = 0; i < obj.length; i++) {
            lineDataHtml += "<li>" + obj[i].name + "&nbsp;&nbsp;<font style='color:#666666'>共" + obj[i].stationNum + "站</font></li>";
        }
        lineDataHtml += "</ul>";
        document.getElementById("lineDataDiv").style.display = "block";
        document.getElementById("lineDataDiv").innerHTML = lineDataHtml;
    }
}

//清空地图及搜索列表
function clearAll() {
   /* map.clearOverLays();*/
    clearSearchPanel();
}

function clearSearchPanel() {
    try
    {
        document.getElementById("searchDiv").innerHTML = "";
        document.getElementById("resultDiv").style.display = "none";
        document.getElementById("statisticsDiv").innerHTML = "";
        document.getElementById("statisticsDiv").style.display = "none";
        document.getElementById("promptDiv").innerHTML = "";
        document.getElementById("promptDiv").style.display = "none";
        document.getElementById("suggestsDiv").innerHTML = "";
        document.getElementById("suggestsDiv").style.display = "none";
        document.getElementById("lineDataDiv").innerHTML = "";
        document.getElementById("lineDataDiv").style.display = "none";
    }
    catch(err)
    {
        //在这里处理错误
    }
}